﻿
Partial Class WebForm_SelfManageVolunteer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Me.BindFV()
            'Dim roleID As Integer = clsAuth.GetRoleIDByName("Employee")
            'If HttpUtility.UrlDecode(Request.Cookies("UserType").Value) = "內部員工" Then
            If clsAuth.GetUser().UserType = My.UserType.Employee Or clsAuth.GetUser().UserType = My.UserType.Person Then
                Response.Redirect("~\Public\OnlyForVolunteer.aspx")
            Else
                Me.AddVolunteer1.InitData()
            End If
        End If
    End Sub

    Protected Sub AddVolunteer1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Saving
        If Me.SaveData = True Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub
End Class

Partial Class WebForm_SelfManageVolunteer
    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim i As Integer
        'If Me.AddVolunteer1.VerificationCode <> Me.AddVolunteer1.VerificationCode Then
        '    MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_InvalidVerificationCode"))
        '    Return False
        'End If

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("update Volunteer set ")
            sql.AppendFormat(" VolunteerName=N'{0}' ", Me.AddVolunteer1.VolunteerName)
            sql.AppendFormat(" ,EcolifeID='{0}' ", Me.AddVolunteer1.EcolifeID)

            If Me.AddVolunteer1.VolunteerPassword <> "" Then
                sql.AppendFormat(",Password='{0}' ", clsAuth.EncryptPassword(Me.AddVolunteer1.VolunteerPassword))
            End If
            'sql.AppendFormat(",Password='{0}' ", clsAuth.EncryptPassword(Me.AddVolunteer1.VolunteerPassword))
            'sql.AppendFormat(",IdentificationID='{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",IdentificationType='{0}' ", Me.AddVolunteer1.IdentificationType)
            sql.AppendFormat(",UpdateDate={0} ", "getdate()")
            sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
            sql.AppendFormat(",Gender='{0}' ", Me.AddVolunteer1.Gender)
            sql.AppendFormat(",Birthday='{0:yyyy/MM/dd}' ", Me.AddVolunteer1.Birthday)
            sql.AppendFormat(",MobilePhone='{0}' ", Me.AddVolunteer1.MobilePhone)
            sql.AppendFormat(",OfficePhone='{0}' ", Me.AddVolunteer1.OfficePhone)
            sql.AppendFormat(",BirthVillageID='{0}' ", Me.AddVolunteer1.BirthRegionID)
            sql.AppendFormat(",BirthAddress='{0}' ", Me.AddVolunteer1.BirthAddress)
            sql.AppendFormat(",SameAddress='{0}' ", Me.AddVolunteer1.SameAddress)
            sql.AppendFormat(",VillageID='{0}' ", Me.AddVolunteer1.RegionID)
            sql.AppendFormat(",Address='{0}' ", Me.AddVolunteer1.Address)
            sql.AppendFormat(",EmergentPerson='{0}' ", Me.AddVolunteer1.EmergentPerson)
            sql.AppendFormat(",EmergentPhone='{0}' ", Me.AddVolunteer1.EmergentPhone)
            sql.AppendFormat(",Email='{0}' ", Me.AddVolunteer1.Email)
            sql.AppendFormat(",EducationLevel='{0}' ", Me.AddVolunteer1.EducationLevel)
            sql.AppendFormat(",OccupationType='{0}' ", Me.AddVolunteer1.OccupationType)

            sql.AppendFormat("where VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '希望隸屬單位
            Dim CheckCount As Integer = 0
            sql.Length = 0
            sql.AppendFormat("delete from WishOrganization where ")
            sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            For i = 0 To Me.AddVolunteer1.WishOrganization.Length - 1
                If (Me.AddVolunteer1.WishOrganization(i) <> "") Then
                    sql.AppendFormat("insert WishOrganization (VolunteerID,OrganizationID) select ")
                    sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WishOrganization(i).ToString())
                    CheckCount += 1
                End If
            Next
            If CheckCount > MyObj.GetParameter("Register", "WishOrganizationCount") Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("OverWishOrganization"))
                Return False
            End If
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '可服務時間
            sql.Length = 0
            sql.AppendFormat("delete from VolunteerWorkTime where ")
            sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            For i = 0 To 2
                sql.AppendFormat("insert VolunteerWorkTime select ")
                sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                Select Case i
                    Case 0
                        sql.AppendFormat(",'{0}' ", "A")
                    Case 1
                        sql.AppendFormat(",'{0}' ", "N")
                    Case 2
                        sql.AppendFormat(",'{0}' ", "P")
                    Case Else
                        sql.AppendFormat(",'{0}' ", "A")
                End Select
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 0))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 1))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 2))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 3))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 4))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 5))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 6))
            Next
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '訓練時數、已服務時數累計(未審核通過前才可更新)
            If Not clsAuth.GetUser().VolunteerStatus = My.VolunteerStatus.Approved And Not clsAuth.GetUser().VolunteerStatus = My.VolunteerStatus.VBooked Then
                '訓練時數
                sql.Length = 0
                sql.AppendFormat("delete from OtherTrainingRecord where ")
                sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                For i = 0 To 2
                    sql.AppendFormat("insert OtherTrainingRecord (VolunteerID,TrainingHour,TrainingType,CreateBy,CreateDate,Active) select ")
                    sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                    sql.AppendFormat(" ,'{0}' ", Me.AddVolunteer1.TrainingHour(i))
                    Select Case i
                        Case 0
                            sql.AppendFormat(",'{0}' ", "B")
                        Case 1
                            sql.AppendFormat(",'{0}' ", "S")
                        Case 2
                            sql.AppendFormat(",'{0}' ", "W")
                        Case Else
                            sql.AppendFormat(",'{0}' ", "B")
                    End Select
                    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
                    sql.AppendFormat(",{0} ", "getdate()")
                    sql.AppendFormat(",'N'")
                Next
                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

                '已服務時數累計
                sql.Length = 0
                sql.AppendFormat("delete from OtherWorkRecord where ")
                sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat("insert OtherWorkRecord (VolunteerID,WorkHour,WorkTimes,CreateDate,CreateBy,Active) select ")
                sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",{0} ", Me.AddVolunteer1.OtherWorkHour)
                sql.AppendFormat(",1")
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",'N'")
                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property VolunteerID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property

    Private ReadOnly Property LoginID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property
End Class